//
//  ViewController.swift
//  瀑布流布局
//
//  Created by monica on 2018/1/9.
//  Copyright © 2018年 xyx. All rights reserved.
//

import UIKit

private let kCollectID = "kCollectID"
class ViewController: UIViewController {
    
    fileprivate lazy var collectionView: UICollectionView = {
        let layout = XYXWaterFallLayout()
        layout.sectionInset = UIEdgeInsetsMake(10 , 10, 10, 10)
        layout.minimumInteritemSpacing = 10
        layout.minimumLineSpacing = 10
        layout.dataSource = self
        
        let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        collectionView.dataSource = self
        
        return collectionView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(collectionView)
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: kCollectID)
    }

  
}

extension ViewController :  UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 100
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCollectID, for: indexPath)
        cell.backgroundColor = UIColor.randomColor()
        return cell
    }
    
}

extension ViewController : XYXWaterFallLayoutDataSource{
    func numberOfCols(_ waterfallLayout: XYXWaterFallLayout) -> Int {
        return 4
    }
    func waterfall(_ waterfall: XYXWaterFallLayout, item: Int) -> CGFloat {
        return CGFloat(arc4random_uniform(100) + 150)
    }
}


